﻿#include <iostream>
#include <string.h>
#include "recivedatabuffer.h"


ReciveDataBuffer::ReciveDataBuffer()
{
    for (int i=0; i<CHANNEL_COUNT; i++)
    {
        m_recvBuffer[i].channelId = i ;
        m_recvBuffer[i].dataLen = 0 ;
        memset(m_recvBuffer[i].dataBuf, 0 , BUFFER_SIZE);
    }
}

ReciveDataBuffer& ReciveDataBuffer::getInstance()
{
    static ReciveDataBuffer s_receiveDataBuffer;
    return s_receiveDataBuffer;
}

/**
 * @brief ReciveDataBuffer::writeDataToBuffer   收到数据后写入buffer
 * @param channelIndex     索引
 * @param buf       数据地址
 * @param len       数据长度
 * @return          写入状态，true=成功，false=失败
 */
bool ReciveDataBuffer::writeDataToBuffer(int channelIndex, char *buf, int len)
{
    if(buf == NULL || len <= 0)
    {
        return false;
    }

    std::unique_lock<std::mutex> lock(m_mutex);

    // buffer overflow，如果数据溢出，那么就直接将这个Buffer内容清掉了，从头开始写--小概率事件会造成数据丢失
    if((m_recvBuffer[channelIndex].dataLen + len) > BUFFER_SIZE)
    {
        memcpy(m_recvBuffer[channelIndex].dataBuf, buf, len);
        m_recvBuffer[channelIndex].dataLen = len;

        std::string strLog = "writeDataToBuffer buffer overflow!!!";
        std::cout << (strLog);
    }
    else
    {
        memcpy(m_recvBuffer[channelIndex].dataBuf + m_recvBuffer[channelIndex].dataLen, buf, len);
        m_recvBuffer[channelIndex].dataLen = len + m_recvBuffer[channelIndex].dataLen;
    }

    return true;
}

/**
 * @brief ReciveDataBuffer::writeBackDataToBuff    将数据重新写回buffer（用于读取数据不完整时重新写回buffer）
 * @param channelIndex         索引
 * @param buf           数据地址
 * @param len           数据长度
 * @return              写入状态，true=成功，false=失败
 */
bool ReciveDataBuffer::writeBackDataToBuff(int channelIndex, char *buf, int len)
{
    if (buf == NULL || len <= 0)
    {
        return false;
    }

    std::unique_lock<std::mutex> lock(m_mutex);

    if((m_recvBuffer[channelIndex].dataLen + len) > BUFFER_SIZE)
    {
        memcpy(m_recvBuffer[channelIndex].dataBuf, buf, len);
        m_recvBuffer[channelIndex].dataLen = len;

        std::string strLog =  std::string("writeDataToBuffer buffer overflow!!!");
        std::cout<<strLog;
    }
    else
    {
        // 将数据写回到buffer头部
        unsigned char dataBuf[BUFFER_SIZE];
        memcpy(dataBuf, buf, len);
        memcpy(&dataBuf[len], m_recvBuffer[channelIndex].dataBuf, m_recvBuffer[channelIndex].dataLen);
        memcpy(m_recvBuffer[channelIndex].dataBuf, dataBuf,len + m_recvBuffer[channelIndex].dataLen);
        m_recvBuffer[channelIndex].dataLen += len;

    }

    return true;
}

/**
 * @brief ReciveDataBuffer::readDataFromBuffer  从buffer中读数据（一次性读完）
 * @param channelIndex             索引
 * @param buf               数据地址
 * @param bufLength         预读数据长度
 * @param getLength         实际读取长度
 * @return                  读取状态，true=成功，false=失败
 */
bool ReciveDataBuffer::readDataFromBuffer(int channelIndex, char *buf, int bufLength, int &getLength)
{
    if(buf == NULL || bufLength <= 0)
    {
        return false;
    }

    std::unique_lock<std::mutex> lock(m_mutex);

    if(bufLength < m_recvBuffer[channelIndex].dataLen)
    {
        return false;
    }

    if (m_recvBuffer[channelIndex].dataLen == 0)
    {
        return false ;
    }

    memcpy(buf, m_recvBuffer[channelIndex].dataBuf, m_recvBuffer[channelIndex].dataLen);
    getLength = m_recvBuffer[channelIndex].dataLen;
    m_recvBuffer[channelIndex].dataLen = 0;

    return true;
}


